﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;

namespace WebRole1
{
    public partial class Diagnostic : BasicPage
    {
        protected string myInfo = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!ctx.MyClsStorageAccount.IsAccountValid)
            {
                Response.Redirect("login.aspx");
            }


            if (!IsPostBack)
            {
                mInit();
            }
        }

        private void mInit()
        {
            try
            {
                txtDeploymentID.Text = ctx.MyClsStorageAccount.DeploymentID;
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }

        }

        protected void btnTrace_Click(object sender, EventArgs e)
        {
            int i = 1;

            System.Diagnostics.Trace.TraceError("{0}:Error", i); i++;

            System.Diagnostics.Trace.TraceWarning("{0}:Warning", i); i++;

            System.Diagnostics.Trace.TraceInformation("{0}:Information", i); i++;

            myInfo = "Generate Trace: Done";

        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            try
            {
                myInfo = ctx.MyDDM.GetDiagnosticInfo();
            }
            catch (Exception ex)
            {
                myInfo = ex.Message;
            }
        }

        private void TraceException(Exception ex)
        {
            myInfo = MyLib.MyAPI.GetErrorMessage(ex);
        }

        protected void btnActivateTrace_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.CollectTrace();
                myInfo = "Activate Trace Logs: Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        protected void btnrestoreConfig_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.RestoreDefaultConfig();
                myInfo = "Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        protected void btnCollectCPU_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.CollectCPU();
                myInfo = "Activate Performance Counter: Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        protected void btnAddPerfCounter_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.AddPerformanceCounter(txtPerfCounter.Text);
                myInfo = "Activate Performance Counter: Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        protected void btnActivateCustomLogs_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.ActivateCustomLog();
                myInfo = "Activate Custom Log: Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }

        }

        protected void btnCustomLog_Click(object sender, EventArgs e)
        {
            try
            {
                ctx.MyDDM.DoCustomLog();
                myInfo = "Create Custom Log: Done";
            }
            catch (Exception ex)
            {
                TraceException(ex);
            }
        }

        protected void btnApplyDeploymentID_Click(object sender, EventArgs e)
        {
            try
            {
                SetDeploymentID();
            }
            catch (Exception ex)
            {
                myInfo = ex.Message;
            }

        }


        private void SetDeploymentID()
        {
            ctx.MyClsStorageAccount.DeploymentID = txtDeploymentID.Text;
            ctx.MyClsStorageAccount.Save();
        }


    }
}